package y2016.date1203.pm.古诗文网采集;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Caijier {
    public static final int MODE_HTML = 0;
    public static final int MODE_PARENT = 1;

    public static class Holder {
        private ArrayList<String> urls;
        /**
         * 分卷
         */
        private String title;
        private ArrayList<String> names;

        public Holder() {
        }

        public Holder(ArrayList<String> urls, String title, ArrayList<String> names) {
            super();
            this.urls = urls;
            this.title = title;
            this.names = names;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public ArrayList<String> getUrls() {
            return urls;
        }

        public void setUrls(ArrayList<String> urls) {
            this.urls = urls;
        }

        public ArrayList<String> getName() {
            return names;
        }

        public void setName(ArrayList<String> names) {
            this.names = names;
        }

        @Override
        public String toString() {
            return "Holder [names=" + urls + ", title=" + title + ", names=" + names
                    + "]";
        }


    }

    public List<Holder> getHolderList(String url, int mode, String charset) {
        if (mode == MODE_PARENT) {
            return parseByPattern(url, charset);
        }
        return parseByHtml(url, charset);

    }

    private List<Holder> parseByPattern(String absolutePath, String charset) {
        String regexContent = "<div[\\s\\S]*</div>";
        Pattern p = Pattern.compile(regexContent);
        Matcher m = p.matcher(readFileToString(absolutePath, charset));
        ArrayList<Holder> holderList = new ArrayList<>();
        while (m.find()) {
            String div = m.group();
            String title = getPTitle(div);
            Holder holder = getDivValue(div, title);
            holderList.add(holder);
        }
        return holderList;
    }

    private String getPTitle(String div) {
        String regex = ">(.*)</span>";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(div);
        m.find();
        return m.group(1);
    }

    @SuppressWarnings("unchecked")
    private <T> T getDivValue(String div, String title) {
        String regex = "(http:.*aspx)\">(.*)<";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(div);
        ArrayList<String> urls = new ArrayList<>();
        ArrayList<String> names = new ArrayList<>();
        while (m.find()) {
            String url = m.group(1);
            String name = m.group(2);
            urls.add(url);
            names.add(name);
        }

        return (T) new Holder(urls, title, names);
    }

    private String readFileToString(String absolutePath, String charset) {
        StringBuilder sb = new StringBuilder();

        try {
            File file = new File(absolutePath);
            InputStream in = new FileInputStream(file);//字节输入流
            InputStreamReader isr = new InputStreamReader(in, charset);//字符输入流
            BufferedReader br = new BufferedReader(isr);
            char[] cbuf = new char[1024];
            int len = 0;
            while ((len = br.read(cbuf)) != -1) {
                sb.append(new String(cbuf, 0, len));
            }
            br.close();
            isr.close();
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return sb.toString();
    }

    /**
     * 通过网页解析
     *
     * @param url
     * @param charset
     * @return
     */
    private List<Holder> parseByHtml(String url, String charset) {
        ArrayList<Holder> holderList = new ArrayList<>();

        Document document = null;
        try {
            document = Jsoup.connect(url).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Elements elements = document.getElementsByClass("bookcont");

        for (Element element : elements) {
            Elements titleE = element.getElementsByTag("strong");
            String title = titleE.text();

            ArrayList<String> urls = new ArrayList<>();
            ArrayList<String> names = new ArrayList<>();

            Elements linkEs = element.getElementsByTag("a");

            for (Element linkE : linkEs) {
                String link = linkE.attr("href");
                String nameText = linkE.text();
                urls.add("http://so.gushiwen.org" + link);
                names.add(nameText);
            }

            holderList.add(new Holder(urls, title, names));
        }

        return holderList;

    }
}

